package com.myapp.ui;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.myapp.R;
import com.myapp.tools.AsyncImgLoader;
import com.myapp.tools.AsyncImgLoader.ImageCallback;
import com.myapp.tools.Tools;
import com.myapp.util.Constant;
import com.myapp.widget.RoundImageView;

public class FriendsDetailActivity extends Activity implements OnClickListener{

	private Bundle bundle = null;
	private AsyncImgLoader asyncImageLoader;
	private RoundImageView uHead;
	private TextView nickname,sex,level,rank,signNum,shareNum,friendsNum,praiseNum;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_friendsdetail);
		bundle = getIntent().getExtras();
		asyncImageLoader = new AsyncImgLoader();
		uHead = (RoundImageView)findViewById(R.id.user_top_head);
		nickname = (TextView)findViewById(R.id.user_nickname);
		sex = (TextView)findViewById(R.id.user_sex);
		level = (TextView)findViewById(R.id.user_level);
		rank = (TextView)findViewById(R.id.user_rank);
		
		signNum = (TextView)findViewById(R.id.usercenter_mysignnum);
		shareNum = (TextView)findViewById(R.id.usercenter_mysharenum);
		friendsNum = (TextView)findViewById(R.id.usercenter_myfriendsnum);
		praiseNum = (TextView)findViewById(R.id.usercenter_mypraisenum);
		
		init();
	}
	
	private void init(){
		
		getCache();
		signNum.setOnClickListener(this);
		shareNum.setOnClickListener(this);
		
		task.execute("");
		
		asyncImageLoader.loadDrawable(this, "http://"+Constant.BACK_IP+"/head/"+bundle.getString("uHead"), new ImageCallback(){
			@Override
			public void imageLoaded(Drawable imageDrawable,
					String imageUrl) {
				if(imageDrawable!=null){
					uHead.setImageDrawable(imageDrawable);
				}
			}
		});
		nickname.setText(bundle.getString("uName"));
	}
	
	AsyncTask<String, Integer, String> task = new AsyncTask<String, Integer, String>(){

		@Override
		protected String doInBackground(String... params) {
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("uid", bundle.getInt("uid"));
			return Tools.toNetWork("http://"+Constant.WS_IP+"/huyanqi-frontWar/rest/user/getUserByUid.json",map);
		}
		
		protected void onPostExecute(String result) {
			if(result!=""){
				try {
					JSONObject data = new JSONObject(result).getJSONObject("userSimpleResp").getJSONObject("result");
					setSharedPreferences(data);
					nickname.setText(data.getString("uNickname"));
					sex.setText(data.getString("uSex"));
					level.setText(data.getString("uLevel"));
					rank.setText(data.getString("uRank")+"λ");
					
					signNum.setText(String.valueOf(data.getInt("uSignNum")));
					shareNum.setText(String.valueOf(data.getInt("uShareNum")));
					friendsNum.setText(String.valueOf(data.getInt("uFriendsNum")));
					praiseNum.setText(String.valueOf(data.getInt("uPraiseNum")));
					
					asyncImageLoader.loadDrawable(FriendsDetailActivity.this, "http://"+Constant.BACK_IP+"/head/"+data.getString("uHead"), new ImageCallback(){
						@Override
						public void imageLoaded(Drawable imageDrawable,
								String imageUrl) {
							if(imageDrawable!=null){
								uHead.setImageDrawable(imageDrawable);
							}
						}
					});
					
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		};
		
	};
	
	private void setSharedPreferences(JSONObject data){
		SharedPreferences userInfo = getSharedPreferences("userInfo_"+bundle.getInt("uid"), MODE_WORLD_READABLE);
		Editor editor = userInfo.edit();
		try {
			editor.putString("uNickname", data.getString("uNickname"));
			editor.putString("uSex", data.getString("uSex"));
			editor.putString("uLevel", data.getString("uLevel"));
			editor.putString("uRank", data.getString("uRank"));
			editor.putString("uSignNum", data.getString("uSignNum"));
			editor.putString("uShareNum", data.getString("uShareNum"));
			editor.putString("uFriendsNum", data.getString("uFriendsNum"));
			editor.putString("uPraiseNum", data.getString("uPraiseNum"));
			editor.putString("uHead", "http://"+Constant.BACK_IP+"/head/"+data.getString("uHead"));
			editor.commit();
			
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	
	private void getCache(){
		SharedPreferences userInfo = getSharedPreferences("userInfo_"+bundle.getInt("uid"), MODE_WORLD_READABLE);
		nickname.setText(userInfo.getString("uNickname", ""));
		sex.setText(userInfo.getString("uSex", ""));
		level.setText(userInfo.getString("uLevel", ""));
		rank.setText(userInfo.getString("uRank","")+"λ");
		
		signNum.setText(userInfo.getString("uSignNum", ""));
		shareNum.setText(userInfo.getString("uShareNum", ""));
		friendsNum.setText(userInfo.getString("uFriendsNum", ""));
		praiseNum.setText(userInfo.getString("uPraiseNum", ""));
		
		asyncImageLoader.loadDrawable(FriendsDetailActivity.this, "http://"+Constant.BACK_IP+"/head/"+bundle.getString("uHead"), new ImageCallback(){
			@Override
			public void imageLoaded(Drawable imageDrawable,
					String imageUrl) {
				if(imageDrawable!=null){
					uHead.setImageDrawable(imageDrawable);
				}
			}
		});
	}

	@Override
	public void onClick(View v) {
		Intent i = null;
		switch (v.getId()) {
		case R.id.usercenter_mysignnum:
			i = new Intent(FriendsDetailActivity.this,SignRecordActivity.class);
			i.putExtras(bundle);
			startActivity(i);
			break;
			
		case R.id.usercenter_mysharenum:
			i = new Intent(FriendsDetailActivity.this,MyShareActivity.class);
			i.putExtras(bundle);
			startActivity(i);
			break;
			
		default:
			break;
		}
	}
	
}
